package xin.ryven.car.android.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Typeface
import android.util.AttributeSet
import android.util.Log
import android.view.View
import androidx.core.content.ContextCompat
import xin.ryven.car.android.lyrics.LrcEntry

class LrcView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private var entries = listOf<LrcEntry>()
    private var currentTime = 0L
    private var currentLine = 0
    
    private val normalPaint = Paint().apply {
        isAntiAlias = true
        textSize = 40f
        textAlign = Paint.Align.CENTER
        color = ContextCompat.getColor(context, android.R.color.darker_gray)
    }
    
    private val highlightPaint = Paint().apply {
        isAntiAlias = true
        textSize = 50f
        textAlign = Paint.Align.CENTER
        color = ContextCompat.getColor(context, android.R.color.black)
        typeface = Typeface.DEFAULT_BOLD
    }
    
    private val lineHeight = 80f
    private var centerY = 0f

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        centerY = h / 2f
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if (entries.isEmpty()) {
            // 如果没有歌词，显示提示文字
            canvas.drawText("暂无歌词", width / 2f, centerY, highlightPaint)
            return
        }

        val centerX = width / 2f
        
        // 绘制当前行上方的歌词
        if (currentLine > 0) {
            for (i in (currentLine - 1) downTo maxOf(0, currentLine - 3)) {
                val y = centerY - ((currentLine - i) * lineHeight)
                canvas.drawText(entries[i].text, centerX, y, normalPaint)
            }
        }
        
        // 绘制当前行
        if (currentLine < entries.size) {
            canvas.drawText(
                entries[currentLine].text,
                centerX,
                centerY,
                highlightPaint
            )
        }
        
        // 绘制当前行下方的歌词
        for (i in (currentLine + 1) until minOf(entries.size, currentLine + 4)) {
            val y = centerY + ((i - currentLine) * lineHeight)
            canvas.drawText(entries[i].text, centerX, y, normalPaint)
        }
    }

    fun updateTime(time: Long) {
        if (entries.isEmpty()) return
        
        currentTime = time
        // 查找当前应该显示的歌词行
        var newLine = 0
        for (i in entries.indices) {
            if (entries[i].time <= time) {
                newLine = i
            } else {
                break
            }
        }
        
        if (newLine != currentLine) {
            currentLine = newLine
            invalidate()
        }
    }

    fun setLrcText(lrcText: String) {
        try {
            entries = LrcEntry.parseLrc(lrcText)
            currentLine = 0
            currentTime = 0
            invalidate()
        } catch (e: Exception) {
            entries = listOf(LrcEntry(0, "暂无歌词"))
            currentLine = 0
            currentTime = 0
            invalidate()
        }
    }

    // 添加调试方法
    fun dumpLrcEntries() {
        entries.forEachIndexed { index, entry ->
            Log.d("LrcView", "Entry $index: time=${entry.time}, text=${entry.text}")
        }
    }
} 